package com.example.luke.cashorderingsystem.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.luke.cashorderingsystem.Adapter.GoodCustomAdapter;
import com.example.luke.cashorderingsystem.Common.CommonActivity;
import com.example.luke.cashorderingsystem.Model.Good;
import com.example.luke.cashorderingsystem.R;
import com.example.luke.cashorderingsystem.Util.GlobalFunction;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 08/12/2016.
 */

public class TestActivity extends CommonActivity {
    private ArrayList<Integer>purchaseCountList;
    @InjectView(R.id.list) ExpandableListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        ButterKnife.inject(this);
        initView();


    }
    Integer currentPurchaseCount;
    Good  respGood;
    ArrayList<Good>shoppingList;
    private void getCurrentPurchaseCountList(){
        for (Good good:shoppingList)
            purchaseCountList.add(good.getPurchaseCount());
    }
    private  void initData(){
        shoppingList=new ArrayList<>();
        Good good=new Good();
        good.setId("001");
        good.setName("002");
        good.setStoredNumber("2");
        good.setPriceIn("1");
        good.setPriceOut("2");

        Good good2=new Good();
        good2.setId("001");
        good2.setName("002");
        good2.setStoredNumber("2");
        good2.setPriceIn("1");
        good2.setPriceOut("2");

        shoppingList.add(good);
        shoppingList.add(good2);

    }


    GoodCustomAdapter shoppingAdapter;
    @Override
    public void initView() {
        super.initView();
        initData();
        purchaseCountList=new ArrayList<>();
        getCurrentPurchaseCountList();




         shoppingAdapter=new GoodCustomAdapter(listView,this,shoppingList);

        shoppingAdapter.setShoppingCartListener(new GoodCustomAdapter.ShoppingCartListener() {
            @Override
            public void reloadPrice() {

            }

            @Override
            public void goBack() {

            }

            @Override
            public void calcVal(HashMap<String,Object>hashMap, int butId) {
            String viewPosition=hashMap.get("position").toString();
                View correspondChildView=(View)hashMap.get("view");


                currentPurchaseCount=purchaseCountList.get(Integer.valueOf(viewPosition));
                respGood=shoppingList.get(Integer.valueOf(viewPosition));


                switch (butId){
                    case R.id.min:
                        if (currentPurchaseCount>1){

                            currentPurchaseCount=currentPurchaseCount-1;

                        }


                        break;

                    case R.id.add:
                        if (currentPurchaseCount<Integer.valueOf(respGood.getStoredNumber()))
                            currentPurchaseCount=currentPurchaseCount+1;

                        break;

                }
                purchaseCountList.set(Integer.valueOf(viewPosition),currentPurchaseCount);


                reloadStoredNumber(correspondChildView,viewPosition);


            }

            @Override
            public void delItem(String viewPosition) {

            }
        });



        listView.setAdapter(shoppingAdapter);
       listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
           @Override
           public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

               return false;
           }
       });


    listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            return false;
        }
    });

        listView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

        int fetch;
        if (listView.getLastVisiblePosition() >=listView.getChildCount()) {
            fetch = listView.getChildCount() - 1 - (listView.getLastVisiblePosition() - Integer.valueOf(groupPosition) );
        } else {
            fetch =Integer.valueOf(groupPosition);

        }
               View view= listView.getChildAt(fetch);
                view.setBackgroundResource(R.drawable.shopping_cart_header_view_1);
                LinearLayout detailInfo=(LinearLayout)view.findViewById(R.id.detailInfo);
                detailInfo.setVisibility(View.GONE);
            }
        });

        listView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                View view= listView.getChildAt(groupPosition);
                view.setBackgroundResource(R.drawable.white_rounded_background);

                LinearLayout detailInfo=(LinearLayout)view.findViewById(R.id.detailInfo);
                detailInfo.setVisibility(View.VISIBLE);

                TextView itemDetail=(TextView)view.findViewById(R.id.itemDetail);
                Good respGood=shoppingList.get(groupPosition);
                String temp="x"+respGood.getPurchaseCount()+" ("+respGood.getTotalPrice()+"元)";
                itemDetail.setText(temp);

            }
        });
    }


    private void reloadStoredNumber(View correspondChildView,String viewPosition){

//        int fetch;
//        if (listView.getLastVisiblePosition() >=listView.getChildCount()) {
//            fetch = listView.getChildCount() - 1 - (listView.getLastVisiblePosition() - Integer.valueOf(viewPosition) );
//        } else {
//            fetch =Integer.valueOf(viewPosition);
//
//        }

        View respView =  correspondChildView;

        String val=String.valueOf(currentPurchaseCount);
        GlobalFunction.printTextByTag(currentPurchaseCount+"");

        respGood.setPurchaseCount(Integer.valueOf(val));


        TextView storedNumber=(TextView)respView.findViewById(R.id.purchaseCount);
        storedNumber.setText(val);



        TextView totalPrice=(TextView)respView.findViewById(R.id.total_price);
        totalPrice.setText(respGood.getTotalPrice()+"元");


//        checkCurrentPrice();


    }

}
